﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

using WpfDataBingding.DevTreeData;
using WpfDataBingding.TaskServiceReference;

namespace WpfDataBingding
{
    public static class MonTaskConfigration
    {

        /// <summary>
        /// 将List<object> listD的项拷贝到List<object> listS
        /// </summary>
        /// <param name="listS"></param>
        /// <param name="listD"></param>
        static public void copyTo(List<UserMonTaskSub> listS, List<UserMonTaskSub> listD)
        {
            foreach (UserMonTaskSub item in listS)
            {
                listD.Add(item);
            }
        }

        /// <summary>
        /// 添加一组设备任务监控
        /// </summary>
        /// <param name="serviceClient"></param>
        /// <param name="dev"></param>
        /// <param name="time"></param>
        static public void addMonDevTaskS(DataBusServiceClient serviceClient, List<Device> devList, List<int> timeList)
        {
            if (serviceClient == null || devList == null || timeList == null || devList.Count != timeList.Count)
            {
                System.Diagnostics.Debug.Assert(true, "addMonDevTask方法的参数出错");
                throw new ApplicationException("addMonDevTask方法的参数出错");
            }
            if (devList.Count == 0)
                return;
            List<UserMonTaskSub> UserMonTaskSubList_main = new List<UserMonTaskSub>(5);

            try
            {
                for (int i = 0; i < devList.Count; i++)
                {
                    List<UserMonTaskSub> UserMonTaskSubList = devList[i].buildMonStructFromDevice_add(timeList[i]);
                    copyTo(UserMonTaskSubList, UserMonTaskSubList_main);
                }

                if (UserMonTaskSubList_main.Count == 0)//没有要添加的任务，返回
                    return;
                UserMonTask userTask_new = new UserMonTask();
                //////////debug
                userTask_new.UserID = "user001";//设置用户ID
                userTask_new.MonTaskList = UserMonTaskSubList_main.ToArray();
                //调用远程服务，添加设备集合的监控
                bool isSuccess = serviceClient.AddUserMonTasks(userTask_new);
                if (isSuccess == false)
                {
                    throw new Exception("远程服务调用返回fasle");
                }

                //修改设备监控状态信息
                foreach (Device dev in devList)
                    dev.setMoniterStatus();
            }
            catch (Exception e)
            {
                string errMsg = "";
                for (int i = 0; i < devList.Count; i++)
                {
                    errMsg += devList[i].DevID;
                    if (i < devList.Count - 1)
                        errMsg += ",";
                }
                throw new ApplicationException("添加对设备" + errMsg + "的监控失败:" + e.Message);
            }
        }

        /// <summary>
        /// 删除一组设备任务监控
        /// </summary>
        /// <param name="serviceClient"></param>
        /// <param name="dev"></param>
        /// <param name="time"></param>
        static public void deleteMonDevTaskS(DataBusServiceClient serviceClient, List<Device> devList)
        {
            if (devList == null || serviceClient == null)
            {
                System.Diagnostics.Debug.Assert(true, "deleteMonDevTask方法的参数出错");
                throw new ApplicationException("deleteMonDevTask方法的参数出错");
            }
            if (devList.Count == 0)
                return;

            List<UserMonTaskSub> UserMonTaskSubList_main = new List<UserMonTaskSub>(5);
            try
            {
                foreach (Device dev in devList)
                {
                    List<UserMonTaskSub> UserMonTaskSubList = dev.buildMonStructFromDevice_remove();
                    copyTo(UserMonTaskSubList, UserMonTaskSubList_main);
                }
                if (UserMonTaskSubList_main.Count == 0)//没有要删的任务，返回
                    return;
                UserMonTask userTask_new = new UserMonTask();
                //////////debug
                userTask_new.UserID = "user001";//设置用户ID
                userTask_new.MonTaskList = UserMonTaskSubList_main.ToArray();
                //调用远程服务，删除设备集合的监控
                bool isSuccess = serviceClient.DeleteUserMonTasks(userTask_new);
                if (isSuccess == false)
                {
                    throw new Exception("远程服务调用返回fasle");
                }

                //修改设备监控状态信息
                foreach (Device dev in devList)
                    dev.cancelMoniterStatus();
            }
            catch (Exception e)
            {
                string errMsg = "";
                for (int i = 0; i < devList.Count; i++)
                {
                    errMsg += devList[i].DevID;
                    if (i < devList.Count - 1)
                        errMsg += ",";
                }
                throw new ApplicationException("删除对设备" + errMsg + "的监控失败:" + e.Message);
            }
        }

        /// <summary>
        /// 添加设备任务监控
        /// </summary>
        /// <param name="serviceClient"></param>
        /// <param name="dev"></param>
        /// <param name="time"></param>
        static public void addMonDevTask(DataBusServiceClient serviceClient, Device dev, int time)
        {
            try 
	        {	        
                UserMonTaskSub[] UserMonTaskSubArray = dev.buildMonStructFromDevice_add(time).ToArray();
                if (UserMonTaskSubArray == null)
                    return;
                UserMonTask userTask_new = new UserMonTask();
                //////////debug
                userTask_new.UserID = "user001";//设置用户ID
                userTask_new.MonTaskList = UserMonTaskSubArray;
                //调用远程服务，删除dev_selected设备的监控
                bool isSuccess=serviceClient.AddUserMonTasks(userTask_new);
                if (isSuccess == false)
                {
                    throw new Exception("远程服务调用返回fasle");
                }
                dev.setMoniterStatus();//修改设备监控状态信息
	        }
	        catch (Exception e)
	        {
                throw new ApplicationException("添加对设备" + dev.DevID + "的监控失败:"+e.Message);		       
	        }
        }

        /// <summary>
        /// 添加设备任务监控,自动创建并调用服务
        /// </summary>
        /// <param name="serviceClient"></param>
        /// <param name="dev"></param>
        /// <param name="time"></param>
        static public void addMonDevTask(Device dev, int time)
        {
           DataBusServiceClient dataBusServiceClient = null;
            try
            {
                dataBusServiceClient = new DataBusServiceClient();
                addMonDevTask(dataBusServiceClient, dev, time);
            }
            catch (Exception e1)
            {
                throw e1;
            }
            finally
            {
                if (dataBusServiceClient != null)
                    dataBusServiceClient.Close();
                dataBusServiceClient = null;
            }
        }

        /// <summary>
        /// 删除设备任务监控
        /// </summary>
        /// <param name="serviceClient"></param>
        /// <param name="dev"></param>
        /// <param name="time"></param>
        static public void deleteMonDevTask(DataBusServiceClient serviceClient, Device dev)
        {
            try 
	        {	        
                UserMonTaskSub[] UserMonTaskSubArray = dev.buildMonStructFromDevice_remove().ToArray();
                if (UserMonTaskSubArray == null)
                    return;
                UserMonTask userTask_new = new UserMonTask();
                ///////////debug
                userTask_new.UserID = "user001";//设置用户ID
                userTask_new.MonTaskList = UserMonTaskSubArray;
                //调用远程服务，删除dev_selected设备的监控
                bool isSuccess=serviceClient.DeleteUserMonTasks(userTask_new);
                if (isSuccess == false)
                {
                    throw new Exception("远程服务调用返回fasle");
                }
                //若服务未出异常
                dev.cancelMoniterStatus();//修改设备监控状态信息
	        }
	        catch (Exception e)
	        {
                throw new ApplicationException("删除对设备" + dev.DevID + "的监控失败:" + e.Message);		
	        }
        }

        /// <summary>
        /// 删除设备任务监控,自动创建并调用服务
        /// </summary>
        /// <param name="serviceClient"></param>
        /// <param name="dev"></param>
        /// <param name="time"></param>
        static public void deleteMonDevTask(Device dev)
        {
            DataBusServiceClient dataBusServiceClient = null;
            try
            {
                dataBusServiceClient = new DataBusServiceClient();
                deleteMonDevTask(dataBusServiceClient, dev);
            }
            catch (Exception e1)
            {
                throw e1;
            }
            finally
            {
                if (dataBusServiceClient != null)
                    dataBusServiceClient.Close();
                dataBusServiceClient = null;
            }
        }

        /// <summary>
        /// 从数据契约的设备类型数组中找出特定名称的类型对象
        /// </summary>
        /// <param name="deviceTypeArray"></param>
        /// <param name="name"></param>
        /// <returns></returns>
        static public DeviceType getDeviceTypeByName(DeviceType[] deviceTypeArray,string name)
        {
            for (int i = 0; i < deviceTypeArray.Length; i++)
            {
                if (deviceTypeArray[i].DevTypeName == name)
                    return deviceTypeArray[i];
            }
            return null;
        }

    }
}
